﻿using System;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class SkillEffective_CureToShield : SkillEffective
    {
        protected override ISerializeCustomPOD OnExecute(SkillEffectiveData data, Unit target, SkillEffectiveContext context, int effectiveIndex, out SkillEffectiveResponse startResp)
        {
            startResp = null;
            var rAttacker = context.skill.executer;
            if (data.effectiveConfig.customConfig is SkillEffective_CureToShield_ConfigData rConfig)
            {
                var nCureFormula = rConfig.cureFormula;
                var fShieldRate = new LFloat(true, rConfig.shieldRate);
                var nDuration = rConfig.shieldDuration;
                
                var rCureResp = DamageController.TakeCure(rAttacker, target, nCureFormula, 1);
                var nOutCure = rCureResp.finalCure - rCureResp.realCure;
                
                var rBuffComp = target.GetComponent<BuffComponent>(ETComponentType.BuffComponent);
                var nShieldValue = (nOutCure * fShieldRate).ToInt();
                rBuffComp.shield.AddShield(nShieldValue, nDuration);
                
                return rCureResp;
            }

            return null;
        }
    }
}